<%@page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@include file="/includes/taglibs.jsp"%>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec" %>
<%@page import="org.springframework.security.core.AuthenticationException"%>
<%@page import="org.springframework.security.web.WebAttributes"%>
<%@page import="javax.security.auth.login.AccountExpiredException"%>
<%@page import="org.springframework.security.authentication.CredentialsExpiredException"%>
<%@page import="org.springframework.security.authentication.DisabledException"%>
<%@page import="org.springframework.security.authentication.LockedException"%>
<%@page import="org.springframework.security.authentication.AccountStatusException"%>
<%@page import="org.springframework.security.authentication.BadCredentialsException"%>
<%@page import="org.springframework.security.authentication.AuthenticationCredentialsNotFoundException"%>
<%@page import="org.springframework.security.core.userdetails.UsernameNotFoundException"%>
<%@page import="com.feib.stms.security.captcha.CaptchaAuthenticationException"%>
<%@page import="com.feib.stms.security.web.authentication.UserIsActivedAuthenticationException"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page import="com.feib.stms.model.User"%>
<%@page import="com.feib.stms.model.Group"%>
<%@page import="com.feib.stms.security.web.authentication.ReLoginAuthenticationException"%>
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<%@ include file="/includes/meta.jsp"%>
	    <title>遠銀網銀股票交割款扣撥系統 || 系統登入</title>
	    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
	    <link rel="stylesheet" type="text/css" media="all" href="<c:url value='/styles/default/mainstyle.css'/>" />
	    <link rel="stylesheet" type="text/css" media="all" href="<c:url value='/styles/default/jquery-ui-1.8.20.custom.css'/>" />
	    
	    <script type="text/javascript" src="<c:url value='/scripts/jquery/jquery-1.7.2.min.js'/>"></script>
	    <script type="text/javascript" src="<c:url value='/scripts/jquery/jquery.bgiframe-2.1.2.js'/>"></script>
	    <script type="text/javascript" src="<c:url value='/scripts/jquery/jquery-ui-1.8.20.custom.min.js'/>"></script>
	    <script type="text/javascript" src="<c:url value='/scripts/jquery/jquery.ui.datepicker-zh-TW.js'/>"></script>
	    <script type="text/javascript" src="<c:url value='/scripts/stms.js'/>"></script>
	    <script type="text/javascript" src="<c:url value='/scripts/loadingScreen.js'/>"></script>
<%

String userId = (String)request.getAttribute("LAST_STMS_FAIL_AUTHENTICATION_USERID");
if (null == userId) userId =(String)request.getSession(true).getAttribute("LAST_STMS_FAIL_AUTHENTICATION_USERID");
String groupNo = (String)request.getAttribute("LAST_STMS_FAIL_AUTHENTICATION_GROUPNO");
if (null == groupNo) groupNo =(String)request.getSession(true).getAttribute("LAST_STMS_FAIL_AUTHENTICATION_GROUPNO");
User user = new User();
user.setGroup(new Group());
user.setUserId(userId);
user.getGroup().setGroupNo(groupNo);
request.setAttribute("user", user);

request.removeAttribute("LAST_STMS_FAIL_AUTHENTICATION_USERID");
request.getSession().removeAttribute("LAST_STMS_FAIL_AUTHENTICATION_USERID");
request.removeAttribute("LAST_STMS_FAIL_AUTHENTICATION_GROUPNO");
request.getSession().removeAttribute("LAST_STMS_FAIL_AUTHENTICATION_GROUPNO");


//登入錯誤訊息顯示
AuthenticationException ae = (AuthenticationException)request.getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
if (null == ae) ae =(AuthenticationException)request.getSession(true).getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
if (ae != null)
{
	
	Class cls = ae.getClass();
	String failedNote = "";
	String note = "";
	if (AccountExpiredException.class.isAssignableFrom(cls))
	{
		failedNote = "帳號過期";
	}
	else if (ReLoginAuthenticationException.class.isAssignableFrom(cls))
	{
		note = ae.getMessage();		
		
	}
	else if (CredentialsExpiredException.class.isAssignableFrom(cls))
	{
		failedNote = "密碼過期";
	}
	else if (DisabledException.class.isAssignableFrom(cls))
	{
		failedNote = "帳號尚未啟用, 或是已被取消刪除";
	}
	else if (LockedException.class.isAssignableFrom(cls))
    {
    	// 帳號已被鎖住
    	failedNote = "帳號已被鎖住";
    }
    else if (AccountStatusException.class.isAssignableFrom(cls))
    {
    	// 帳號狀態其他問題
    	failedNote = "帳號狀態其他問題";
    }
    else if (BadCredentialsException.class.isAssignableFrom(cls))
    {
    	// 密碼錯誤
    	failedNote = "帳號或密碼有誤";
    }
    else if (AuthenticationCredentialsNotFoundException.class.isAssignableFrom(cls))
    {
    	// 密碼錯誤或無資料庫無對應密碼
    	failedNote = "帳號或密碼有誤";
    }
    else if (UsernameNotFoundException.class.isAssignableFrom(cls))
    {
    	// 帳號不存在
    	failedNote = "帳號或密碼有誤";
    }
    else if (CaptchaAuthenticationException.class.isAssignableFrom(cls))
    {
    	// 驗證碼錯誤
    	failedNote = "驗證碼錯誤";
    }
    else if (UserIsActivedAuthenticationException.class.isAssignableFrom(cls))
    {
    	failedNote = "使用者已登入";
    }
    else if (AuthenticationException.class.isAssignableFrom(cls))
    {
    	// 授權認證其他有問題
    	failedNote = "認證資訊有問題";
    }   
    else
    	failedNote = ae.getMessage();
    	
	request.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
	request.getSession().removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
	pageContext.setAttribute("STMS_LOGIN_FAILED_NOTE", failedNote);
	pageContext.setAttribute("STMS_LOGIN_NOTE",note);
}	
%>
	
	    <script type="text/javascript">
	    <!--
	    $(document).ready(function() {
	    	
	    	$('#doLoginBtn').click(function() {	    			
	    		var userName = $('#userName').val();
	    		var password = $('#password').val();
	    		$('#j_username').val('####_conjunction_' + userName);
	    		
	    		var errorMessage = '';
	    			
	    		if (userName == '')
	    		{
	    		    if (errorMessage != '') errorMessage = errorMessage + '<br>';
	    		    errorMessage = errorMessage + '使用者名稱不可空白!!';
	    		}	
                if (password == '')
                {
                    if (errorMessage != '') errorMessage = errorMessage + '<br>';
                    errorMessage = errorMessage + '密碼不可空白!!';
                }   
                
                if (errorMessage != '')
                {
                    showErrorMessageDialog(errorMessage);
                    return false;
                }		    		
	    		return true;
	        });
	        
	        <c:if test="${not empty STMS_LOGIN_FAILED_NOTE}">showErrorMessageDialog('<c:out value="${STMS_LOGIN_FAILED_NOTE }"/>');</c:if>
	    	<c:if test="${not empty STMS_LOGIN_NOTE}">showMessageDialog('<c:out value="${STMS_LOGIN_NOTE }"/>');</c:if>
	  
	    });
	    
	    //-->
	    </script>
	</head>
	<body>
			<div id="loginTable">
					<form id="loginFrom" action="<c:url value='/stms_security_login'/>" method="post" onsubmit="javascript: waitingDialog({});">
					
	        	
	                <input id="j_username" name="j_username" value="" type="hidden" />
	                
	                <table>
	                	<tr>
	                		<td>帳號</td>
	                		<td colspan="3"><input id="userName" maxlength="10" name="user.userId" value="<c:out value='${user.userId}'/>" type="text" size="12" /></td>
	                	</tr>
	                	<tr>
	                		<td>密碼</td>
	                		<td colspan="3"><input id="password" maxlength="15" name="j_password" type="password" size="12" /></td>
	                	</tr>
	                	<tr>
	                		<td>驗證碼</td>
	                		<td>
	                			<input type='text' name='j_captcha_response' value='' maxlength="5" size="12"/>
	                		</td>
	                		<td align="left" width="70">
	                			<img id="jcaptchaImage" src="<c:url value='/jcaptcha.jpg'/>" height="35" width="70"/>	                			
	                		</td>
	                		<td>
	                			<input type="button" value='重新產生驗證碼' onclick='javascript:d = new Date(); $("#jcaptchaImage").attr("src", "<c:url value="/jcaptcha.jpg"/>?"+d.getTime());'/>
	                		</td>
	                	</tr>
	                	<tr>
	                		<td></td>
	                		<td colspan="3">
	                			<input id="doLoginBtn" name="doLoginBtn" value="確認登入" type="submit" />
	                			<input id="doResetBtn" name="doResetBtn" value="清除重填" type="reset" />
	                			
	                		</td>
	                	</tr>
	                </table>
	                </form>
	       </div>
	       
	       <div id="messageDiv"></div>
	       
	       <div id="loadingScreen"></div>
               
	</body>
</html>